#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Weazel.Gezel.Model
{
  /// <summary>
  /// This class it the abstract representation of
  /// an ip-block in the gezel modelling language.
  /// </summary>
  public partial class IPBlock : Block
  {   

    /// <summary>
    /// The type of ipblock. Can be ram, rom etc.
    /// </summary>
    protected string ipType;
    public string IpType
    {
      get
      {
        return ipType;
      }
      set
      {
        ipType = value;
      }
    }

    /// <summary>
    /// The list of parameter-value pairs used to 
    /// configure the ipblock prior to simulation.
    /// </summary>
    protected List<string> ipParameters;
    public List<string> IpParameters
    {
      get
      {
        return ipParameters;
      }
    }

		private bool inSystem = false;

    /// <summary>
    /// True iff ipblock is part of a
    /// system. Only ipblocks which
    /// are in a system will be run during
    /// simulation.
    /// </summary>
    public bool InSystem
    {
      get
      {
        return inSystem;
      }
      internal set
      {
        inSystem = value;     
      }
    }

		public IPBlock(Model parent, string name, Position startPosition, Position endPosition)
			: this(parent, name)
    {
			this.startPosition = startPosition;
			this.endPosition = endPosition;
    }

    /// <summary>
    /// Constructs a new IPBlock with the specified Model as parent and
    /// local name.
    /// </summary>
    /// <param name="parent">the parent model</param>
    /// <param name="name">the local name of the ipblock</param>
    public IPBlock(Model parent, string name) : 
			base(parent, name)
    {
      ipParameters = new List<string>();
    }

    /// <summary>
    /// Runs through the input and output ports and returns
    /// the port with the specified port position or null
    /// if the port was not found.
    /// </summary>
    /// <param name="position"></param>
    /// <returns>the port with the specified position</returns>
    public new Port GetPortAtPosition(int position)
    {
      // Console.WriteLine("Getting port at pos {0} in: {1} out: {2}", position, inPorts.Count, outPorts.Count);

      foreach (InPort ip in inPorts.Values)
        if (ip.DeclarationPosition == position)
          return ip;

      foreach (OutPort op in outPorts.Values)
        if (op.DeclarationPosition == position)
          return op;

      return null; // port was not found
    }

		/// <summary>
		/// Given the local name of a port this method
		/// returns the actual object - if it exists.
		/// </summary>
		/// <param name="name">local name of port to get</param>
		/// <returns></returns>
		public new Port GetPortByLocalName(string name)
		{
			foreach (InPort port in InPorts.Values)
				if (port.LocalName == name)
					return port;

			foreach (OutPort port in outPorts.Values)
				if (port.LocalName == name)
					return port;

			return null;
		}
  }
}
